
'use strict';
//从配置文件获取服务器信息
var configs = require("./../configs_local");
var config = configs.game_server();
var http_service = require("./http_service");

var db = require('../utils/db');
db.init(configs.mysql());

//api
var _ = require("lodash");

var generate_num_control = 0;
var name_index = 0;
var timer = null;
var nickname = {
    0: '$酒魂=9',
    1: '回眸一笑傻了',
    2: '-,',
    3: '528...',
    4: 'love !',
    5: '53度',
    6: '茅台纯香',
    7: 'hello妹',
    8: '叶子#',
    9: '喜夜风',
    10: '*宝贝雪儿*',
    11: '小小波斯猫',
    12: '^.^绝尘无望',
    13: '一杯就倒.',
    14: '&强哥',
    15: '四喜丸子',
    16: '樱花开那年',
    17: '^~^',
    18: 'LLLL',
    19: '这料猪猪很爱吃',
    20: '安多拉之梦',
    21: '蔡大妈',
    22: '北彊战神',
    23: '米米粉碎机',
    24: '春风十里不如我',
    25: '山秀水甜酒醇',
    26: '..999..',
    27: '^卫浴',
    28: '酒都雀圣',
    29: '只玩几把',
    30: '啥酒能走心',
    31: '杀报中',
    32: '刘姐',
    33: '阿妹',
    34: '杯酒释兵权',
    35: '与您接吻瞬间,醉了',
    36: '啥鸡都有',
    37: '卖野味的小伙',
    38: '来一丢丢',
    39: '*^*tt',
    40: 'thsejiffe',
    41: ' \\\\',
    42: '一杯醇香.',
    43: '农用设备销售',
    44: 'ccc ',
    45: ' 飞来一斧 ',
    46: 'APAP ',
    47: ' %^&$ ',
    48: '潇洒张工',
    49: '0123456',
    50: '你动作快点',
    51: 'ios-',
    52: '走一个',
    53: '酒糟很香',
    54: '窖藏几岁',
    55: 'cup',
    56: '朵朵',
    57: '@#@666',
    58: '白色杯与黑色杯',
    59: '菜农周氏',
    60: '你的明后天',
    61: '。。。；；',
    62: 'ufo&$',
    63: '走入国酒门',
    64: '太阴不喜欢太阳',
    65: 'wowo...o',
    66: '5858258^*^',
    67: '蔡蔡',
    68: '入秋了  ',
    69: '羞羞答答你管我',
    70: '解愁佳酿',
    71: '慧慧小妖',
    72: '李冰冰2',
    73: '隔壁老曹',
    74: '萌萌儿。。',
    75: '',
    76: '周师傅的手艺 ',
    77: '** gg >>',
    78: '山人几多',
    79: '######### '
};

var headimgs = {
    0: 'http://wx.qlogo.cn/mmhead/Q3auHgzwzM6NJHPwAJeA6Q6ZsFqXynsXAX7bqXxXVJ76gzUEq7l0Cg/0',
    1: 'http://wx.qlogo.cn/mmhead/Q3auHgzwzM6NJHPwAJeA6Q6ZsFqXynsXibfGgpg0J8eBPeNd3ic8AWvQ/0',
    2: 'http://wx.qlogo.cn/mmhead/PiajxSqBRaEL9f7WNGoMaoADhrjUVZicnO5mePuf2V9gBv25jgnicoic6A/0',
    3: 'http://wx.qlogo.cn/mmhead/Sqqm3oJYw2iaLdO3ICWuAOP6ibYnOykCOMzXPhHAYVowTRDGEOyEWnvg/0',
    4: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOQbSNMrOTVxTdSRpmjfo0fJ5a8jtW5HNWUVeQ7M0WSqGwJHcAtmOEXRsibEyuSv2ZsCic47Vr41xt3y6ng6g1Bsm4/0',
    5: 'http://wx.qlogo.cn/mmopen/jlsQU6HiclYQBnKv7xk1x0ejkUmx9dkWCR3oA697pliaSj1sonibEXmq7yPc0cDOKgibDjZWLtyPwQFWvlhHvshwt74J2Bnk6jXn/0',
    6: 'http://wx.qlogo.cn/mmopen/PiajxSqBRaELMKRDkdOok5GUakt4af8JGOfW4NKic2NA0okJaIKJXuyqOjOaKl96HgWERUebVlaJvOp5MX4g2GeA/0',
    7: 'http://wx.qlogo.cn/mmopen/xPKCxELaaj7uibJILLMBM4xIuDjeCcX9bb24wgDCJ5CU4egmViaO2QR2YqEl5Y7v9cibDoxz0m5pBDYjohNTgEYEsSbRcAoJmCN/0',
    8: 'http://wx.qlogo.cn/mmopen/xPKCxELaaj6DNs9ndn5p5v0uaKLIQdeYh8ia6vQduAtnOQ9tiakML0fN21vobap99e37n2v2GnicFYyApCDaGgEZuzG3IaX5cdN/0',
    9: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOTamrNRPsVsh6m86Go1Wfmkc257LcVrLtTnoeHIGPl5ibefXnWDhfNbXibR750mdl4XeTs17pB7I792waJ6tZxMrQ/0',
    10: 'http://wx.qlogo.cn/mmopen/dWYcndbpDnarGOlLqCqn2iaXQgogia9Ilvbibsa76IoiajKcQ7XnVgRRMFlgwKd48m5tLSCcDskjEjVXVWgdARbFn8rxnVbJ9jzVH3B3lEyqyhU/0',
    11: 'http://wx.qlogo.cn/mmopen/PiajxSqBRaEIJf6qS3EtRVvm7eQdQw6ZzYYvrzm6RmXu8xUB5WfRibrricRrBnOrNibicQ6BiaLffNd6vvahvEdY0amA/0',
    12: 'http://wx.qlogo.cn/mmopen/jlsQU6HiclYRmHJRxuakB0Ucuic73GcJcWVeicNIrQm4cHRiaAzs24iazSCCkwuBZ2F0ku8vOSRd0KHSX3o1IDmOQy3228mpXF2Q5/0',
    13: 'http://wx.qlogo.cn/mmopen/PiajxSqBRaEI7tRo1qaicMVnUSH6xlpaDEspQtKIfdwlUXYBzBvE3gps0BOoRJSITlYc0IepiafIBguBwUu4LAJIw/0',
    14: 'http://wx.qlogo.cn/mmopen/Q3auHgzwzM53VwvBnaoBicXP12JpokAmZxgXFn4wSD7Uxd5nGCSTs7ggfc89eSWyw7eJ9g5ChBbGHhoTt1mCObNAj2JGSC5AojDhyKF427nQ/0',
    15: 'http://wx.qlogo.cn/mmopen/ajNVdqHZLLD8TpZwfTicZ17gjwa4uHt7oSic8wHe5HfV86awyCtWBWiaPHeSsX6HC2kF1X7K0pMCf4bGBGeicnQHibib0Wykdq2fib4ibKYgupweeDc/0',
    16: 'http://wx.qlogo.cn/mmopen/Q3auHgzwzM6NJHPwAJeA6Q6ZsFqXynsXmJO04vIiaG8sjkbPckwrxpw/0',
    17: 'http://wx.qlogo.cn/mmopen/PiajxSqBRaEL9f7WNGoMaoADhrjUVZicnOgJrlsiczxY81jAqW2OB6kEQ/0',
    18: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOSM5orvticVdXYepJCQenkgzxDa6nYCvviaUZudRkAm9LxRcLic8Yuk39uVjmjZ7uXJPY98WSpiaae8xYBIJHiafV5Tg/0',
    19: 'http://wx.qlogo.cn/mmopen/PiajxSqBRaEL9f7WNGoMaoADhrjUVZicnOKvHQuQNpnMdiat4GpMib5icPA/0',
    20: 'http://wx.qlogo.cn/mmhead/Sqqm3oJYw2iaLdO3ICWuAOP6ibYnOykCOMibT8IVgSicO3YUcD3Hrv07FA/0',
    21: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOSM5orvticVdXYepJCQenkgzxDa6nYCvviaUZudRkAm9LxSqTiaLrZ8Q8mX6bbcy0rvmiayOvQyXsyj6sUgZoQQ8UibB/0',
    22: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOTamrNRPsVsh8IdYPYuJGiacxZhKc2aKs8swtUACjexEKqfc2rtOkAiaR9NNjeVMDnAa2TFnnElvH9DPZTT6Eg4BI/0',
    23: 'http://wx.qlogo.cn/mmopen/Q3auHgzwzM78ibQw6wGG6ic1Y4coM2icq9vvMLkkxVReiac9w5656nfSKCI8bBdu5Q0DPemFdjnTniaWYfRqN7fQXTWdp5yyibeyFtTziaEI4ibqJTE/0',
    24: 'http://wx.qlogo.cn/mmopen/PiajxSqBRaEI7tRo1qaicMVnUSH6xlpaDEspQtKIfdwlUXYBzBvE3gps0BOoRJSITlYc0IepiafIBguBwUu4LAJIw/0',
    25: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFUicwTMzicE0QLjic40dJERQHf2baeh8oRIT1bMJfYmicbPzmKbYTwOG3X9xe99XdVNV1YntJdSXD9L9/0',
    26: 'http://wx.qlogo.cn/mmhead/xzvVYaqtSnzKn5z8oqL3iaDOhQNXYKwtibpgmIWAnvKRUxqfn23PGojw/0',
    27: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    28: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    29: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    30: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    31: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    32: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIp0f9Ujfl1xTtx0OQ4WWYxhvhRKIxQHY2Igrvk0cb61KYeicEKOeVQmR4ibr70OhAXEVY4M6D0r1k5aTHxjhusG5W/0',
    33: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    34: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    35: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    36: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    37: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    38: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    39: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOQ18s8sOrAgKve3jTWLA2QgZWFIgKfrp6Zf9s1hKicSCrcKboo3Id4hmLwL7wd7J5MlgRLjWTFd70NJibEMlJxQ8q/0',
    40: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    41: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOQSFjkAyny4xrlqqrQ4x9uBjjJls288f0axcrB9o7pP6jd0Rgqpz1jUia2yp3AiaDUo06GIAFTUrS2AXaWYFvvV1K/0',
    42: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    43: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOQSFjkAyny4xrlqqrQ4x9uBjjJls288f0axcrB9o7pP6jd0Rgqpz1jUia2yp3AiaDUo06GIAFTUrS2AXaWYFvvV1K/0',
    44: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    45: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOQSFjkAyny4xrlqqrQ4x9uBjjJls288f0axcrB9o7pP6jd0Rgqpz1jUia2yp3AiaDUo06GIAFTUrS2AXaWYFvvV1K/0',
    46: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    47: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOQSFjkAyny4xrlqqrQ4x9uBjjJls288f0axcrB9o7pP6jd0Rgqpz1jUia2yp3AiaDUo06GIAFTUrS2AXaWYFvvV1K/0',
    48: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    49: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOQSFjkAyny4xrlqqrQ4x9uBjjJls288f0axcrB9o7pP6jd0Rgqpz1jUia2yp3AiaDUo06GIAFTUrS2AXaWYFvvV1K/0',
    50: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    51: 'http://wx.qlogo.cn/mmopen/dmXmGXic6ZOQSFjkAyny4xrlqqrQ4x9uBjjJls288f0axcrB9o7pP6jd0Rgqpz1jUia2yp3AiaDUo06GIAFTUrS2AXaWYFvvV1K/0',
    52: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    53: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    54: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    55: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    56: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    57: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    58: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    59: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    60: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    61: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    62: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    63: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    64: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    65: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    66: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    67: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    68: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    69: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    70: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    71: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    72: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    73: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    74: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    75: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    76: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    77: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    78: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
    79: 'http://wx.qlogo.cn/mmopen/ibsoMicdEPGIqibpnfcpbmrFa1p7hsgrUjeD04WwFHh6iaKibmjlU7ibpGJmriaZUHnibl7SphQe99F5EU01YYVjwicP0ABgWDFanen47/0',
};

//生成账号
function generateWXAccount() {
    var account_head = 'wx_okB7Pw';
    var rand_num = '0123456789';
    var rand_sign = '-_';
    var rand_char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    var len = 31 - account_head.length;
    for (let i = 0; i < len; ++i) {
        var rand_type = Math.floor(Math.random() * 100);
        if (rand_type < 80) {
            let rand_char_index = Math.floor(Math.random() * 100) % 52;
            account_head = account_head + rand_char.substr(rand_char_index, 1);
        } else if (rand_type >= 80 && rand_type < 98) {
            let rand_num_index = Math.floor(Math.random() * 100) % 10;
            account_head = account_head + rand_num.substr(rand_num_index, 1);
        } else {
            let rand_sign_index = Math.floor(Math.random() * 100) % 2;
            account_head = account_head + rand_sign.substr(rand_sign_index, 1);
        }
    }
    return account_head;
}

function generateUserId() {
    var userId = "";
    for (var i = 0; i < 6; ++i) {
        if (i == 0) {
            userId = 1 + Math.floor(Math.random() * 9);
        }
        else {
            userId += (Math.floor(Math.random() * 10)).toString();
        }
    }
    return userId;
}

function generateOnlyUserId(account, name, coins, gems, sex, headimgurl, callback) {
    ++generate_num_control;
    if (generate_num_control > 2) {
        callback(0, false);
    }
    var userId = generateUserId();
    db.is_userid_exist(userId, function (data) {
        if (data == 0) {
            db.generate_user(userId, account, name, 999, coins, gems, sex, headimgurl, function (ret1, ret2) {
                callback(ret1, ret2);
            });
        }
        else {
            console.log("repeat generate user id!");
            generateOnlyUserId(account, name, coins, gems, sex, headimgurl, function (ret1, ret2) {
                callback(ret1, ret2);
            });
        }
    });
}

function generateAccNum(num, nickname, headimgs, callback) {
    if (nickname[name_index] == null) {
        clearInterval(timer);
        console.log("clear interval stop generate robot");
        return;
    }
    db.is_name_exist(nickname[name_index], function (ret1, ret2) {
        if (ret1 > 0) {
            console.log("exist ", nickname[name_index]);
            ++name_index;
            return;
        }
        for (let i = 0; i < num; ++i) {
            generate_num_control = 0;
            var account = generateWXAccount();
            console.log(account);
            db.get_user_data(account, function (ret) {
                if (ret == null) {
                    var coins = 50000;
                    var gems = 18;
                    db.is_user_exist(account, function (userid, ret) {
                        if (!ret) {
                            var sex = 1 + Math.floor(Math.random() * 2);
                            var name = nickname[name_index];
                            var headimg = headimgs[name_index];
                            ++name_index;
                            generateOnlyUserId(account, name, coins, gems, sex, headimg, function (ret1, ret2) {
                                callback(ret1, ret2);
                            });
                        }
                    });
                }
            });
        }
    });
}

/*
timer = setInterval(function () {
    generateAccNum(1, nickname, headimgs, function (ret1, ret2) {
        console.log("ret1,ret2", ret1, ret2);
    });
}, 10000);*/

function generateRoomId() {
    var roomId = "";

    for (var i = 0; i < 6; ++i) {
        if (i == 0) {
            roomId = 1 + Math.floor(Math.random() * 9);
        }
        else {
            roomId += (Math.floor(Math.random() * 10)).toString();
        }
    }
    return roomId;
}

function generateResult(nickname, index, callback) {
    var name = [];
    name.push(nickname[index * 2]);
    name.push(nickname[index * 2 + 1]);

    db.get_vip_user_data(name, function (ret) {
        if (ret != null) {
            console.log(ret);
            var i = 0;
            var uuid = 0;
            var roomId = 0;
            var roomInfo = {};
            roomInfo.roomNum = 0;
            var seatNum = 2;

            roomInfo.begin_time_tmp = 0;
            roomInfo.end_time_tmp = 0;
            var timer2 = setInterval(function () {
                try {
                    if (i % 8 == 0) {
                        i = 0;
                        roomId = generateRoomId();
                        uuid = Date.now() + roomId;
                        roomInfo.createTime = Date.now();

                        db.is_room_exist(roomId, function (ret2) {
                            if (ret2) {
                                generateResult(nickname, index);
                                console.log("repeat generate room id!");
                            } else {
                                generateOneRound(i, roomInfo, seatNum, uuid, roomId, ret, function (ret2) {
                                    console.log("插入结果记录成功! A", roomId, roomInfo.numOfGames);
                                    i = roomInfo.numOfGames;
                                });
                            }
                        });
                    } else {
                        generateOneRound(i, roomInfo, seatNum, uuid, roomId, ret, function (ret2) {
                            console.log("插入结果记录成功! ", roomId, roomInfo.numOfGames);
                            i = roomInfo.numOfGames;
                            if (i == 8) {
                                ++roomInfo.roomNum;
                                if (i * roomInfo.roomNum == 160) {
                                    clearInterval(timer2);
                                    console.log("CLEAR TIMER");
                                    return;
                                }
                            }
                        });
                    }
                } catch (error) {
                    console.log("generate error ", nickname, i, error);
                }
            }, 100000);
        }
    });
}

function generateOneRound(i, roomInfo, seatNum, uuid, roomId, ret, callback) {
    var winner = Math.floor(Math.random() * seatNum);
    roomInfo.uuid = uuid;
    roomInfo.id = roomId;
    roomInfo.nextButton = winner;
    roomInfo.numOfGames = 0;
    roomInfo.seats = new Array(seatNum);

    let bankerScore = 0;
    for (let j = 0; j < seatNum; ++j) {
        let seat = roomInfo.seats[j] = {};
        seat.userid = ret[j].userid;
        seat.name = ret[j].name;
        if (j == winner) {
            seat.score = seatNum * (1 + Math.floor(Math.random() * 30));
            bankerScore = seat.score;
            console.log("winner score == ", seat.userid, seat.score);
        }
    }
    for (let j = 0; j < seatNum; ++j) {
        let seat = roomInfo.seats[j];
        if (j != winner) {
            if (seatNum == 2) {
                seat.score = -1 * bankerScore;
            } else {
                seat.score = -1 * (bankerScore / (seatNum - 1));
            }
            console.log("other score == ", seat.userid, seat.score);
        }
    }
    roomInfo.numOfGames = i + 1;
    db.generate_eachround_history(roomInfo, seatNum, function (ret) {
        if (ret) {
            callback(true);
        } else {
            callback(false);
        }
    });
}

//1.5分钟执行一次
var date = null;
var generateRes = {};

setInterval(function () {
    date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var dateFormat = year + '-' + month + '-' + day;
    var nowTime = Date.now();

    if (generateRes[dateFormat] === undefined) {
        console.log("one day only one time");

        var randTime = Math.floor(Math.random() * 3) + 9;
        date.setHours(randTime);
        randTime = Math.floor(Math.random() * 60);
        date.setMinutes(randTime);
        generateRes[dateFormat] = { time: date.getTime(), finish: false };

        console.log(dateFormat, generateRes[dateFormat]);

    } else if (!generateRes[dateFormat].finish && nowTime - generateRes[dateFormat].time >= 0) {

        console.log("one time generate once");
        generateRes[dateFormat].finish = true;

        for (let i = 0; i < 40; ++i) {
            setTimeout(function () { generateResult(nickname, i); }, 60000 * (i + 1));
        }
    }
}, 3000);
